﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;

namespace ZF.BLL.ACChar
{
    public class AdminChar
    {
        public static List<PeopleModel> olList = new List<PeopleModel>();//在线列表
        [WebMethod]
        [ScriptMethod]
        public string RecMsg(string myGuid, string osGuid)
        {
            var people = GetModel(myGuid);
            List<MsgModel> list = new List<MsgModel>();
            if (people != null)
            {
                list=people.MsgList.Where(p => p.Sender.Guid == osGuid).ToList();
                people.MsgList.RemoveAll(p => list.Contains(p));
            }
            return MsgToJson(list);
             
        }

        public string MsgToJson(List<MsgModel> list)
        {
           var reulst= list.Select(p => new
                {
                    sender = p.Sender.Name,
                    datetime = p.SendTime.ToString(),
                    msg = p.Msg,
                }).ToList();
           return  JsonConvert.SerializeObject(reulst);
        }
        [WebMethod]
        [ScriptMethod]
        public string SendMsg(string myGuid, string osGuid, string msg)
        {
            var my = GetModel(myGuid);
            var oside = GetModel(osGuid);
            bool success = true;
            string resultMsg = "";

            if (my == null && success)
            {
                resultMsg = "您当前已断开网络，请重新刷新后再尝试发送";
                success = false;
            }
            if (oside == null && success)
            {
                resultMsg = "对方已离开";
                success = false;
            }
            if (success)
            {
                oside.MsgList.Add(new MsgModel
                {
                    Msg = msg,
                    SendTime = DateTime.Now,
                    Sender = my,
                });
            }
            return JsonConvert.SerializeObject(new
            {
                result = success,
                msg = resultMsg,
            });
        }
        [WebMethod]
        [ScriptMethod]
        public string GetOnLineList(string myGuid)
        {
            var my = GetModel(myGuid);
            var list = new List<PeopleModel>();
            if (my != null)
            {
                list = olList.Where(p => p.IsService != my.IsService).ToList();
            }
            var result = list.Select(p => new
                {
                    name = p.Name,
                    guid = p.Guid,
                }).ToList();
            return JsonConvert.SerializeObject(result);

        }
        [WebMethod]
        [ScriptMethod]
        public bool RegOnLine(string guid,string name)
        {
             
            var r = new Random();
            name += r.Next(100).ToString();

            return _RegOnLine(guid, name, false);

        }
        [WebMethod]
        [ScriptMethod]
        public bool RegOnLine(string guid, bool isService)
        {
            string name = isService ? "客服" : "游客";
            var r = new Random();
            name += r.Next(100).ToString();

            return _RegOnLine(guid, name, isService);

        }
        [WebMethod]
        [ScriptMethod]
        public bool RegOnLine(int memberId, bool isService)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var member = dc.US_Member.FirstOrDefault(p => p.MemberId == memberId);
                if (member != null)
                {


                    return _RegOnLine(member.MemberId.ToString(), "客服" + (new Random()).Next(100).ToString(), isService);
                }
                else
                    return RegOnLine((Guid.NewGuid().ToString()), isService);
            }
        }
        private bool _RegOnLine(string guid, string name, bool isService)
        {
            var people = GetModel(guid);
            if (people != null)
            {
                return true;
            }
            else
            {
                olList.Add(new PeopleModel()
                {
                    Guid = guid,
                    IsService = isService,
                    Name = name,
                    UpdateTime = DateTime.Now,
                });
                return true;
            }

        }
        [WebMethod]
        [ScriptMethod]
        public bool RegDownLine(string guid)
        {
            var people = GetModel(guid);
            if (people != null)
                olList.Remove(people);
            return true;
        }
        [WebMethod]
        [ScriptMethod]
        public bool Keep(string guid)
        {
            var people = GetModel(guid);
            if (people != null)
            {
                people.UpdateTime = DateTime.Now;
                return true;
            }
            else return false;
        }
        private PeopleModel GetModel(string guid)
        {
            var people = olList.FirstOrDefault(p => p.Guid == guid);
            return people;
        }
        static AdminChar()
        {
            Action<object> checkTimeOut = o =>
            {
                while (true)
                {
                    olList.RemoveAll(p => p.UpdateTime.AddSeconds(60*3) < DateTime.Now);
                    Thread.Sleep(1000 * 30);
                }
            };
            ThreadPool.QueueUserWorkItem(new WaitCallback(checkTimeOut));
        }
    }
    public class PeopleModel
    {
        public PeopleModel() { MsgList = new List<MsgModel>(); }
        public string Name { get; set; }
        public string Guid { get; set; }
        public bool IsService { get; set; }//是否为客服
        public DateTime UpdateTime { get; set; }
        public List<MsgModel> MsgList { get; set; }
        
    }
    public class MsgModel
    {
        public PeopleModel Sender{get;set;}
        public string Msg { get; set; }
        public DateTime SendTime { get; set; }
    }
     

}
